********Table R.1 - Mexico********

eststo clear


*Load data and create differences
cd "$reploc/data/created_data"
use mex_full, clear

sort pid_link mxfls
drop if mxfls==.
keep if panel==1

keep rbuc* mxfls pid_link inflation imp_age panel birthyr birthstate yearprov grbr???? mex_gr???? yearinterview grbrprovAvg_lam0co3 grbrprovSD_lam0co3

reshape wide rbuc*  inflation imp_age yearinterview grbrprovAvg_lam0co3 grbrprovSD_lam0co3, i(pid_link) j(mxfls)


local varlist1 rbuc rbuc_nga inflation grbrprovAvg_lam0co3 grbrprovSD_lam0co3


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'3-`var'2
}

*Generate cohort by birthyear variables to cluster on
gen coh_prov_clust=yearprov

*Do this for primary sample
gen primarysample1=0
replace primarysample1=1 if rbuc_ngadif!=. & grbrprovAvg_lam0co3dif!=. & grbrprovSD_lam0co3dif!=. & inflationdif!=. &  birthyr>=1925 & coh_prov_clust!=.

egen primarysample=total(primarysample1), by(pid_link)

keep if primarysample==1


*Regressions
local x rbuc_nga
local rhsvari oldmexonly
local rhsvaruse co3
local usei grbr

*Lambda value seeds are obtained by maximizing the likelihood obtained by estimating linearized versions of the model with different values of lambda. The code to find these seeds is available upon request
local lam1vec 30 N 30
local lam2vec N 40 30

drop if `x'dif==.

forval subreg=1/3 {

preserve

local lam1int : word `subreg' of `lam1vec'
local lam2int : word `subreg' of `lam2vec'

local hola1=substr("`lam1int'",1,3)
local hola2=substr("`lam2int'",1,3)


if "`lam1int'"!="N" & "`hola1'"!="neg" {
local lam1=round(`lam1int'/100,0.1)
}

if "`lam2int'"!="N" & "`hola2'"!="neg" {
local lam2=round(`lam2int'/100,0.1)
}

if "`lam1int'"!="N" & "`hola1'"=="neg" {
local lam1inta=substr("`lam1int'",4,.)
local lam1=(-1)*round(`lam1inta'/100,0.1)
}

if "`lam2int'"!="N" & "`hola2'"=="neg" {
local lam2inta=substr("`lam2int'",4,.)
local lam2=(-1)*round(`lam2inta'/100,0.1)
}

*Province growth
local allgrs `usei'1941
forval i=1942/2014 {
local allgrs `allgrs' `usei'`i'
}

*Mexico growth
local allgrsmex mex_gr1925
forval i=1926/2014 {
local allgrsmex `allgrsmex' mex_gr`i'
}

*Column 1
if `subreg'==1 {

gen mis=missing(inflationdif,imp_age2,imp_age3,birthyr,`x'dif,yearinterview2,yearinterview3,coh_prov_clust)
drop if mis==1

foreach v of varlist grbr???? { 
drop if missing(`v') 
}

cd "$reploc/programs"
eststo: nl lambda_spec1_`rhsvaruse'_mex @ `x'dif `allgrs' inflationdif imp_age2 imp_age3 birthyr yearinterview2 yearinterview3 `allgrsmex' if panel==1, parameters(bcons bgr lam binf) initial(bcons 0 bgr 0 lam `lam1' binf 0) cluster(coh_prov_clust)

}

if `subreg'==2 {

gen mis=missing(inflationdif,imp_age2,imp_age3,birthyr,`x'dif,yearinterview2,yearinterview3,coh_prov_clust)
drop if mis==1
foreach v of varlist grbr???? { 
	drop if missing(`v') 
}

cd "$reploc/programs"
eststo: nl lambda_spec2_`rhsvaruse'_mex @ `x'dif `allgrs' inflationdif imp_age2 imp_age3 birthyr yearinterview2 yearinterview3 `allgrsmex' if panel==1, parameters(bcons bsd lam binf) initial(bcons 0 bsd 0 lam `lam2' binf 0) cluster(coh_prov_clust)


}

if `subreg'==3 {

gen mis=missing(inflationdif,imp_age2,imp_age3,birthyr,`x'dif,yearinterview2,yearinterview3,coh_prov_clust)
drop if mis==1
foreach v of varlist grbr???? { 
	drop if missing(`v') 
}

cd "$reploc/programs"
eststo: nl lambda_spec3_`rhsvaruse'_mex @ `x'dif `allgrs' inflationdif imp_age2 imp_age3 birthyr yearinterview2 yearinterview3 `allgrsmex' if panel==1, parameters(bcons bgr bsd lam binf) initial(bcons 0 bgr 0 bsd 0 lam `lam1' binf 0) cluster(coh_prov_clust)


}

restore

}

cd "$reploc/results"	
esttab using TableR1_Mexico.tex, star(* 0.1 ** 0.05 *** 0.01) b(2) se(2) replace
